package com.bootcamp;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.ServletException;

import atg.commerce.profile.CommercePropertyManager;
import atg.core.util.Address;
import atg.core.util.StringUtils;
import atg.droplet.DropletException;
import atg.projects.b2cstore.B2CProfileFormHandler;
import atg.repository.MutableRepository;
import atg.repository.MutableRepositoryItem;
import atg.repository.RepositoryException;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import atg.userprofiling.Profile;

/**
 * 
 * @author alei
 * 
 */
public class BootCampProfileFormHandler extends B2CProfileFormHandler {

	private String mMonthOfBirth;
	private String mDayOfBirth;
	private String mYearOfBirth;

	public void setMonthOfBirth(String pMonthOfBirth) {
		this.mMonthOfBirth = pMonthOfBirth;
	}

	public String getMonthOfBirth() {
		return this.mMonthOfBirth;
	}

	public void setDayOfBirth(String pDayOfBirth) {
		this.mDayOfBirth = pDayOfBirth;
	}

	public String getDayOfBirth() {
		return this.mDayOfBirth;
	}

	public void setYearOfBirth(String pYearOfBirth) {
		this.mYearOfBirth = pYearOfBirth;
	}

	public String getYearOfBirth() {
		return this.mYearOfBirth;
	}

	protected void preCreateUser(DynamoHttpServletRequest pRequest,
			DynamoHttpServletResponse pResponse) throws ServletException,
			IOException {
		super.preCreateUser(pRequest, pResponse);

		String month = getMonthOfBirth();
		String day = getDayOfBirth();
		String year = getYearOfBirth();

		if (StringUtils.isEmpty(month)) {
			addFormException(new DropletException(
					"The value 'month of birth' is required and must be supplied "));
		}
		if (StringUtils.isEmpty(day)) {
			addFormException(new DropletException(
					"The value 'day of birth' is required and must be supplied "));
		}
		if (StringUtils.isEmpty(year)) {
			addFormException(new DropletException(
					"The value 'year of birth' is required and must be supplied "));
		}

		if (!getFormError()) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = dateFormat.parse(year + "-" + month + "-" + day);
				getValue().put("dateOfBirth", date);
			} catch (ParseException e) {
				addFormException(new DropletException("parse date occur error"));
			}
		}

	}

	public boolean handleUpdatePaymentInfo(DynamoHttpServletRequest pRequest,
			DynamoHttpServletResponse pResponse) throws ServletException,
			IOException, RepositoryException {
		boolean flag = false;
		HashMap cardValues = (HashMap) this.getEditValue();
		CommercePropertyManager cpmgr = (CommercePropertyManager) this
				.getProfileTools().getPropertyManager();
		Profile profile = this.getProfile();

		MutableRepository repository = (MutableRepository) getProfileTools()
				.getProfileRepository();
		MutableRepositoryItem profileItem = (MutableRepositoryItem) this
				.getProfileItem();
		MutableRepositoryItem billingAddress = (MutableRepositoryItem) profile
				.getPropertyValue("billingAddress");
		billingAddress.setPropertyValue("firstName",
				cardValues.get("firstName"));
		billingAddress.setPropertyValue("lastName", cardValues.get("lastName"));
		billingAddress.setPropertyValue("address1", cardValues.get("address1"));
		billingAddress.setPropertyValue("address2", cardValues.get("address2"));
		billingAddress.setPropertyValue("city", cardValues.get("city"));
		billingAddress.setPropertyValue("state", cardValues.get("state"));
		billingAddress.setPropertyValue("country", cardValues.get("country"));
		billingAddress.setPropertyValue("postalCode",
				cardValues.get("postalCode"));
		profileItem.setPropertyValue("billingAddress", billingAddress);

		MutableRepositoryItem defaultCard = (MutableRepositoryItem) profile
				.getPropertyValue(cpmgr.getDefaultCreditCardPropertyName());
		if (defaultCard == null) {
			MutableRepositoryItem newCard = repository.createItem(cpmgr
					.getCreditCardItemDescriptorName());
			newCard.setPropertyValue("creditCardType",
					cardValues.get("creditCardType"));
			newCard.setPropertyValue("creditCardNumber",
					cardValues.get("creditCardNumber"));
			newCard.setPropertyValue("expirationMonth",
					cardValues.get("expirationMonth"));
			newCard.setPropertyValue("expirationDayOfMonth",
					cardValues.get("expirationDayOfMonth"));
			newCard.setPropertyValue("expirationYear",
					cardValues.get("expirationYear"));
			repository.addItem(newCard);
			profileItem.setPropertyValue("defaultCreditCard", newCard);
			repository.updateItem(profileItem);
		} else {
			defaultCard.setPropertyValue("creditCardType",
					cardValues.get("creditCardType"));
			defaultCard.setPropertyValue("creditCardNumber",
					cardValues.get("creditCardNumber"));
			defaultCard.setPropertyValue("expirationMonth",
					cardValues.get("expirationMonth"));
			defaultCard.setPropertyValue("expirationDayOfMonth",
					cardValues.get("expirationDayOfMonth"));
			defaultCard.setPropertyValue("expirationYear",
					cardValues.get("expirationYear"));
			profileItem.setPropertyValue("defaultCreditCard", defaultCard);
			repository.updateItem(profileItem);
		}
		flag = true;
		return flag;
	}

}

